/**
 * Created by YHQ on 2016-03-25.
 */
angular.module('hyt.zddzCtrl',[])

    //通过站点查询账务
  .controller('SearchZddzCtrl',function($scope,$state,ZdFactory,$cordovaDatePicker,Storage,$ionicLoading,$ionicPopup,$filter){
      //选择站点
      $scope.chooseZd=function(){
          var alertPopup = $ionicPopup.alert({
              title:'请选择到达站点',
              scope:$scope,
              template: '<ion-list  ng-repeat="item in zdList"><ion-radio ng-bind="item.bmmc" style="text-align: center;margin-top: 3px;" ng-click="choose(item.bmmc,item.bm_id)"></ion-radio></ion-list>',
              okText:'取消'
          });
          alertPopup.then(function(res) {
              if (res) {

              } else {

              }
          });
          $scope.choose=function(bmmc,bm_id){
              $scope.bm_id=bm_id;
              $scope.ddz=bmmc;
              alertPopup.close();
          };
      };

      //开始时间
      var t = new Date();
      var iToDay=t.getDate();
      var iToMon=t.getMonth();
      var iToYear=t.getFullYear();
      var iTDate=new Date(iToYear,iToMon,(iToDay-30));
      $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
      $scope.showStartDate = function() { //选择日期
          var optionsStart = {
              date: iTDate,
              mode: 'date',
              maxDate: new Date($scope.endDate) - 10000
          };
          datePicker.show(optionsStart, function(d) {
              if (!isNaN(d.getTime())) {  // valid date
                  $scope.$apply(function () {
                      iTDate=d;
                      $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
                  });
              }
          });
      };
      //结束时间
      var endTime = new Date();
      $scope.endDate=$filter('date')(endTime,'yyyy-MM-dd');
      $scope.showEndDate = function(){
          console.log('EndDate');
          var optionsEnd = {
              date : endTime,
              mode : 'date',
              minDate: new Date($scope.startDate) - 10000
          };
          datePicker.show(optionsEnd,function(d) {
              if (!isNaN(d.getTime())) {  // valid date
                  $scope.$apply(function () {
                      endTime=d;
                      $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
                  });
              }
          });

      };
    var sid=Storage.get('sid');
    var user_id=Storage.get('user_id');
    var yh_id=Storage.get('yh_id');
    var dw_id=Storage.get("dw_id");
    var zd_id=Storage.get("zd_id");
    ZdFactory.queryZd(dw_id,sid,user_id,yh_id,zd_id);
    //获得该公司的站点列表
    $scope.$on('List.zdList',function(){
        var list=ZdFactory.getZd();
        $scope.zdList=list.ACTION_INFO.RESULT_LIST;
        console.log($scope.zdList);
    })

    //点击查询按钮传递参数并跳转到zddzlist页面
    $scope.queryCw=function(ddzid,ddzmc,kssj,jssj,dw_id){
      if(ddzid==''||ddzid==null){
          $ionicLoading.show({
              noBackdrop: true,
              template: "请选择到达站",
              duration: 1500
          });
      }else {
          $state.go('zddzlist',{ddz:ddzid,kssj:kssj,jssj:jssj,dw_id:dw_id,ddzmc:ddzmc});
      }
    }
  })

    //站点对账列表
  .controller('ZddzListCtrl',function($scope,ZddzListFactory,$stateParams,Storage){
      var sid=Storage.get('sid');
      var user_id=Storage.get('user_id');
      var yh_id=Storage.get('yh_id');
      var dw_id=Storage.get("dw_id");
      var zd_id=Storage.get("zd_id");
      var bm_id=Storage.get('bm_id');
      var bmmc=Storage.get('bmmc');
      $scope.showloading = true;
      $scope.cyd={
          yqCyd:'',
          cyzt:'',
          yqCyd1:'',
          cyzt1:''
      };
      //xx站到xx站
      $scope.sfzmc=bmmc;
      $scope.ddzmc=$stateParams.ddzmc;

      $scope.dz=function(){
          if($scope.cyd.yqCyd==true){
              $scope.cyd.cyzt='100';
          }else{
              $scope.cyd.cyzt='';
          }
          ZddzListFactory.queryList(sid,user_id,yh_id,dw_id,$stateParams.ddz,$stateParams.kssj,$stateParams.jssj,$scope.cyd.cyzt,bm_id);
      };
      $scope.dzfx=function(){
          if($scope.cyd.yqCyd1==true){
              $scope.cyd.cyzt1='100';
          }else{
              $scope.cyd.cyzt1='';
          }
          ZddzListFactory.queryListBack(sid,user_id,yh_id,dw_id,$stateParams.ddz,$stateParams.kssj,$stateParams.jssj,$scope.cyd.cyzt1,bm_id);
      };
      //通过$stateParams接收serachzddz页面传过来的参数，初始化站点间财务列表
      ZddzListFactory.queryList(sid,user_id,yh_id,dw_id,$stateParams.ddz,$stateParams.kssj,$stateParams.jssj,$scope.cyd.cyzt,bm_id);
      ZddzListFactory.queryListBack(sid,user_id,yh_id,dw_id,$stateParams.ddz,$stateParams.kssj,$stateParams.jssj,$scope.cyd.cyzt1,bm_id);

      $scope.$on('ZddzList.zddzUpdated', function() {
          var zddzListFactory=ZddzListFactory.getCaiwu();
          $scope.zddzData = zddzListFactory.data;
          console.log($scope.zddzData);
          $scope.showloading=false;
      });
      $scope.$on('ZddzListBack.zddzUpdated', function() {
          var zddzListFactory=ZddzListFactory.getCaiwuBack();
          $scope.zddzDataBack = zddzListFactory.data;
          console.log($scope.zddzDataBack);
          $scope.showloading=false;
      });
      //下拉刷新
      $scope.doRefresh = function(){
          ZddzListFactory.queryList(sid,user_id,yh_id,dw_id,$stateParams.ddz,$stateParams.kssj,$stateParams.jssj);
          $scope.$broadcast('scroll.refreshComplete');
      };
      $scope.doRefresh1 = function(){
          ZddzListFactory.queryListBack(sid,user_id,yh_id,dw_id,$stateParams.ddz,$stateParams.kssj,$stateParams.jssj);
          $scope.$broadcast('scroll.refreshComplete');
      };

      //上拉加载更多
      $scope.loadMore = function(){
          ZddzListFactory.getMoreCaiwu();
          $scope.$broadcast('scroll.infiniteScrollComplete');
      };

      //下一页
      $scope.hasNextPage =function(){
          return ZddzListFactory.hasNextPage();
      }

  })

